package Controller.admin.refund;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import Controller.admin.AdminBaseServlet;
import Entity.Book;
import Entity.Invoice;
import Services.WebService;

public class RefundServlet extends AdminBaseServlet {    
    @Override
    protected void processRequest(HttpServletRequest request, HttpServletResponse response, WebService webServices, String action) throws ServletException, IOException{
    	request.setAttribute("view", action);
    	
      	if(action!=null && action.equals("refund")){
    		listRefundRequests(request,response,webServices,action);
    	}
      	else if(action!=null && action.equals("accept")){
      		acceptRefundRequest(request,response,webServices,action);
    	}
      	else if(action!=null && action.equals("reject")){
      		rejectRefundRequest(request,response,webServices,action);
    	}
      	else if(action==null || action.equals("list")){
    		listInvoices(request,response,webServices,action);
    	}

    }
    
    private void listInvoices(HttpServletRequest request, HttpServletResponse response, WebService webServices, String action){
    	ArrayList<Invoice> invoiceList = webServices.getEntityManager().getInvoices();
    	ArrayList<Book> bookList = new ArrayList<Book>();
    	
    	for(int i=0;i<invoiceList.size();i++){
    		bookList.add(webServices.getEntityManager().getBookFromInvoice(invoiceList.get(i).getId()));
    	}
    	
    	request.setAttribute("bookList", bookList);
    	request.setAttribute("invoiceList", invoiceList);
		this.forward(request, response, webServices, action, "/admin/refund/list.jsp");
    }
    
    private void listRefundRequests(HttpServletRequest request, HttpServletResponse response, WebService webServices, String action){
    	ArrayList<Invoice> invoiceList = webServices.getEntityManager().getRefundInvoices();
    	ArrayList<Book> bookList = new ArrayList<Book>();
    	
    	for(int i=0;i<invoiceList.size();i++){
    		bookList.add(webServices.getEntityManager().getBookFromInvoice(invoiceList.get(i).getId()));
    	}
    	
    	request.setAttribute("bookList", bookList);
    	request.setAttribute("invoiceList", invoiceList);
		this.forward(request, response, webServices, action, "/admin/refund/list.jsp");
    }
    
    private void acceptRefundRequest(HttpServletRequest request, HttpServletResponse response, WebService webServices, String action){
    	String invoiceID = request.getParameter("id");
    	Invoice invoice = webServices.getEntityManager().getInvoice(Integer.valueOf(invoiceID));
    	
    	invoice.setRefund(false);
    	invoice.setIsRefunded(true);
    	
    	webServices.getEntityManager().updateInvoice(invoice);

    	request.setAttribute("request_invoice", invoice);
    	listRefundRequests(request,response,webServices,action);
    }
    
    private void rejectRefundRequest(HttpServletRequest request, HttpServletResponse response, WebService webServices, String action){
    	String invoiceID = request.getParameter("id");
    	Invoice invoice = webServices.getEntityManager().getInvoice(Integer.valueOf(invoiceID));
    	
    	invoice.setRefund(false);
    	invoice.setIsRefunded(false);
    	
    	webServices.getEntityManager().updateInvoice(invoice);
    	
    	request.setAttribute("request_invoice", invoice);
    	listRefundRequests(request,response,webServices,action);
    }
    
}